package com.alongstudio.account.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alongstudio.account.commons.exception.BusinessException;
import com.alongstudio.account.entity.Account;
import com.alongstudio.account.service.IAccountService;

@Service("AccountService")
public class AccountService extends BaseService implements IAccountService {

	private String className = AccountService.class.getName();
	

	@Override
	public List<Account> findAccounts(String userId, Byte type) throws BusinessException {
		List<Account> result = null;
//		try {
//			String hql = "from Account a where a.clientUser.id=" + userId;
//			if (type != null) {
//				hql += " and a.accountType=" + type;
//			}
//			result = dao.query(hql);
//		}
//		catch (Exception e) {
//			throw new BusinessException(className, e);
//		}
		return result;
	}

	@Override
	public String createAccount(Account account) throws BusinessException {
		try {
			dao.create(account);
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return account.getId();
	}

	@Override
	public boolean updateAccount(Account account) throws BusinessException {
		try {
			dao.update(account);
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return true;
	}

	@Override
	public boolean deleteAccount(String id) throws BusinessException {
		try {
			dao.deleteByPk(Account.class, id);
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return true;
	}

	@Override
	public Account findById(String id) throws BusinessException {
		Account account = null;
		try {
			account = (Account) dao.queryByPk(Account.class, id);
		}
		catch (Exception e) {
			throw new BusinessException(className, e);
		}
		return account;
	}

	// @Override
	// public List<Account> findNormalAccounts(Integer userId, byte type) throws
	// BusinessException {
	// List<Account> result = null;
	// try {
	// String hql = "from Account a where a.clientUser.id=" + userId +
	// "and a.accountType =" + type;
	// result = dao.query(hql);
	// }
	// catch (Exception e) {
	// throw new BusinessException(className, e);
	// }
	// return result;
	//
	// }

	@Override
	public Map findInitAccounts(String userId) throws BusinessException {
		Map result = new HashMap();
//		try {
//			String hql = "from Account a where a.clientUser.id=" + userId;
//			List<Account> allAccounts = dao.query(hql);
//			result.put("allAccounts", allAccounts);// 所有账户
//			
//			String hql2 = hql + " and a.accountType in(" + StringUtil.concatArray(SystemConstant.ACCOUNT_INCOME) + ")";
//			List<Account> incomeAccounts = dao.query(hql2);
//			result.put("incomeAccounts", incomeAccounts);// 收入账户
//			
//			hql2 = hql + " and a.accountType in(" + StringUtil.concatArray(SystemConstant.ACCOUNT_EXPENDITURE) + ")";
//			List<Account> expenditureAccounts = dao.query(hql2);
//			result.put("expenditureAccounts", expenditureAccounts);// 支出账户
//			
//			hql2 = hql + " and a.accountType in(" + StringUtil.concatArray(SystemConstant.ACCOUNT_DEBT) + ")";
//			List<Account> debtAccounts = dao.query(hql2);
//			result.put("debtAccounts", debtAccounts);//欠债账户
//			
//			hql2 = hql + " and a.accountType in(" + StringUtil.concatArray(SystemConstant.ACCOUNT_LOAN_OUT) + ")";
//			List<Account> loanOutAccounts = dao.query(hql2);
//			result.put("loanOutAccounts", loanOutAccounts);//借出账户
//
//			hql2 = hql + " and a.accountType ="+SystemConstant.ACCOUNT_TYPE_CREDITOR;
//			List<Account> creditorAccounts = dao.query(hql2);
//			result.put("creditorAccounts", creditorAccounts);//债权账户
//		}
//		catch (Exception e) {
//			throw new BusinessException(className, e);
//		}
		return result;

	}

}
